package com.xinlan.xinlanmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 解决跨域问题
 * @author thinkpad
 * @Auther ZhangGuoXin
 * @Date 2024/1/11
 */
@Configuration
public class XinlanmallCorsConfiguration {

    @Bean
    public CorsWebFilter coreFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //1、配置跨域
        corsConfiguration.addAllowedHeader("*"); //放行哪些原始请求头部信息
        corsConfiguration.addAllowedMethod("*"); //放行哪些请求方式
        corsConfiguration.addAllowedOrigin("*");  //放行哪些原始域
        corsConfiguration.setAllowCredentials(true); //是否发送 Cookie

        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}
